const mysql = require('mysql2/promise');
const pool = mysql.createPool({
  host: 'localhost',
  user: 'root',
  password: 'mysql_password',
  database: 'orm',
  namedPlaceholders: true,
  connectionLimit: 10,      //连接池的链接限制数量
  waitForConnections: true,   //是否等待连接
  queueLimit: 10,           //等待数量，
});

// 事务，添加数据方式
async function preformTransaction(){
  const connection = await pool.getConnection();
  try {
    await connection.beginTransaction();
    // 事务，添加数据方式
    await connection.query(`insert into tags(name,id) values('good',2)`)
    await connection.query(`insert into tags(name,id) values('top',3)`)
    await connection.commit();
  }catch (err){
    // 失败撤回操作
    await connection.rollback();
    console.error(err);
  }finally{
    // 不管成功与否释放线程池
    connection.release();
  }
}
preformTransaction().catch(console.error).finally(()=>{
  process.exit(0);
})
